import pandas as pd
import numpy as np
from math import ceil, modf
from datetime import datetime

INFINITE = 99999


def xlsx_to_csv(xlsx, csv):
    data_xls = pd.read_excel(xlsx, index_col=0)
    data_xls.to_csv(csv, encoding='utf-8')


def preprocess(data):
    data = data
    data.drop(index=data[data['record_id'] == 351].index, inplace=True)
    data.drop(index=data[data['record_id'] == 456].index, inplace=True)
    data.drop(index=data[data['record_id'] == 8].index, inplace=True)
    data.drop(index=data[data['record_id'] == 38].index, inplace=True)
    data.drop(index=data[data['record_id'] == 315].index, inplace=True)
    data.drop(index=data[data['record_id'] == 488].index, inplace=True)
    data.drop(index=data[data['record_id'] == 259].index, inplace=True)
    data.drop(index=data[data['record_id'] == 147].index, inplace=True)
    data.drop(index=data[data['record_id'] == 185].index, inplace=True)
    data.drop(index=data[data['record_id'] == 271].index, inplace=True)
    data.drop(index=data[data['record_id'] == 378].index, inplace=True)
    data.drop(index=data[data['record_id'] == 505].index, inplace=True)
    data.drop(index=data[data['record_id'] == 507].index, inplace=True)
    data.drop(index=data[data['record_id'] == 540].index, inplace=True)
    data.drop(index=data[data['record_id'] == 252].index, inplace=True)
    data.drop(index=data[data['record_id'] == 294].index, inplace=True)
    data.drop(index=data[data['record_id'] == 421].index, inplace=True)
    data.drop(index=data[data['record_id'] == 443].index, inplace=True)
    data.drop(index=data[data['record_id'] == 489].index, inplace=True)
    data.loc[data['record_id'] == 243, 'bmij'] = 1.0
    data.loc[data['record_id'] == 369, 'bmij'] = 1.0
    data.loc[data['record_id'] == 419, 'bmij'] = 3.0
    data.loc[data['record_id'] == 143, 'height'] = 1.58
    data.loc[data['time'] == 'NK', 'time'] = '1'
    data.loc[data['time'] == 'MFOLFOX 4次', 'time'] = '4'
    data.loc[data['time'] == 'CAPEOX1次、FOLFOX2次', 'time'] = '3'
    data.loc[data['time'] == '口服卡培他滨', 'time'] = '1'
    data.loc[data['feces'] == '2001-02-03 00:00:00', 'feces'] = '1'
    data.loc[data['feces'] == '0-1', 'feces'] = '0.5'
    data.loc[data['feces'] == '1~2', 'feces'] = '1.5'
    data.loc[data['feces'] == '44324', 'feces'] = '1'
    data.loc[data['feces'] == '44417', 'feces'] = '1'
    data.loc[data['feces'] == '44260', 'feces'] = '1'
    data.loc[data['feces'] == '44200', 'feces'] = '1'
    data.loc[data['feces'] == '44354', 'feces'] = '1'
    data.loc[data['feces'] == '44199', 'feces'] = '1'
    data.loc[data['feces'] == '44387', 'feces'] = '1'
    data.loc[data['feces'] == '44322', 'feces'] = '1'
    data.loc[data['feces'] == '44385', 'feces'] = '1'
    data.loc[data['feces'] == '44291', 'feces'] = '1'
    data.loc[data['feces'] == '44259', 'feces'] = '1'
    data.loc[data['feces'] == '44198', 'feces'] = '1'
    data.loc[data['feces'] == '44230', 'feces'] = '1'
    data.loc[data['kenel'] == '五型', 'kenel'] = '5'
    data.loc[data['kenel'] == '五', 'kenel'] = '5'
    data.loc[data['kenel'] == '四型', 'kenel'] = '4'
    data.loc[data['kenel'] == '四', 'kenel'] = '4'
    data.loc[data['kenel'] == '六型', 'kenel'] = '6'
    data.loc[data['kenel'] == '六', 'kenel'] = '6'
    data.loc[data['kenel'] == '一型', 'kenel'] = '1'
    data.loc[data['kenel'] == '三型', 'kenel'] = '3'
    data.loc[data['kenel'] == '三', 'kenel'] = '3'
    data.loc[data['kenel'] == '二型', 'kenel'] = '2'
    data.loc[data['kenel'] == '5、6', 'kenel'] = '5'
    data.loc[data['nrs_score'] == '五型', 'nrs_score'] = '5'
    data.loc[data['rbc'] == '3,77', 'rbc'] = '3.77'
    data.loc[data['rbc'] == '4,2', 'rbc'] = '4.2'
    data.loc[data['rbc'] == '4,43', 'rbc'] = '4.43'
    data.loc[data['wbc'] == '9,8', 'wbc'] = '9.8'
    data.loc[data['wbc'] == '4,.40', 'wbc'] = '4.40'
    data.loc[data['palb'] == '261..4', 'palb'] = '261.4'
    data.loc[data['alb'] == '31,1', 'alb'] = '31.1'
    data.loc[data['alb'] == '36..9', 'alb'] = '36.9'
    data.loc[data['record_id'] == 431, 'cdzb'] = 4.0
    data.loc[data['db'] == '七', 'db'] = '7'
    data.loc[data['record_id'] == 20, 'mzsc'] = 3.0
    data.loc[data['record_id'] == 44, 'mzsc'] = 3.0
    data.loc[data['record_id'] == 198, 'mzsc'] = 7.0
    data.loc[data['record_id'] == 99, 'qgbgsc'] = 8.0
    data.loc[data['record_id'] == 408, 'mzks'] = '2021-07-01 11:05:00'
    data.loc[data['record_id'] == 6, 'mzks'] = '2020-11-09 13:40:00'
    data.loc[data['record_id'] == 42, 'xbl'] = 0
    data.loc[data['record_id'] == 63, 'cdcd'] = 'NK'
    data.loc[data['record_id'] == 171, 'cdcd'] = 'NK'
    data.loc[data['record_id'] == 171, 'cdcd'] = 'NK'
    data.loc[data['record_id'] == 221, 'cdcd'] = '20'
    data.loc[data['record_id'] == 153, 'pbsk'] = INFINITE
    data.loc[data['record_id'] == 340, 'pbsk'] = INFINITE
    data.loc[data['record_id'] == 10, 'pbsk'] = INFINITE
    return data


def one_hot(df, label_flag='day', int_flag='i', cut='cut', model='bia', dummy='dummy'):
    def check(series):
        print(series.shape)
        print(series.value_counts(sort=True))
        if series.isnull().any():
            print(series[series.isnull().values == True])
        else:
            print("None")

    def category_feature(df):
        return df.values

    def continue_feature(series):
        series = series.astype('float')
        return np.expand_dims(series.values, axis=1)

    def bi_feature(series):
        series = series.astype('int')
        mapping = {
            1: 0,
            2: 1
        }
        series = series.map(mapping)
        return np.expand_dims(series.values, axis=1)

    def zd(series):
        series = series.fillna(2.0)
        series = series.astype('int')
        mapping = {
            1: 'jca',
            2: 'zca',
            3: 'jca+zca',
        }
        series = series.map(mapping)

        if dummy == 'dummy':
            cat_dtype = pd.CategoricalDtype(categories=['jca', 'zca', 'jca+zca'])
            series = series.astype(cat_dtype)
            series = pd.get_dummies(series, prefix='zd')
            return series.values
        else:
            return np.expand_dims(series.values, axis=1)

    def bmij(series):
        series = series.astype('int')
        mapping = {
            1: 'zc',
            2: 'xs',
            3: 'cz',
            4: 'fp'
        }
        series = series.map(mapping)

        if dummy == 'dummy':
            cat_dtype = pd.CategoricalDtype(categories=['zc', 'xs', 'cz', 'fp'])
            series = series.astype(cat_dtype)
            series = pd.get_dummies(series, prefix='bmij')
            return series.values
        else:
            return np.expand_dims(series.values, axis=1)

    def nation(series):
        series = series.fillna(1.0)
        series = series.astype('int')
        mapping = {
            1: 'hz',
            2: 'other',
            3: 'other',
            4: 'other'
        }
        series = series.map(mapping)

        if dummy == 'dummy':
            cat_dtype = pd.CategoricalDtype(categories=['hz', 'other'])
            series = series.astype(cat_dtype)
            series = pd.get_dummies(series, prefix='nation')
            return series.values
        else:
            return np.expand_dims(series.values, axis=1)

    def j(df):
        series = np.ones(df['j'].shape[0])
        series = pd.Series(series)

        for index, row_j, row_medical in zip(range(df.shape[0]), df['j'], df['medical']):
            if row_j == 2.0:
                series.loc[index] = row_medical
            else:
                series.loc[index] = 0.0

        series = series.astype('int')
        mapping = {
            0: 'none',
            1: 'other',
            2: 'tnb',
            3: 'gxy',
            4: 'other',
            5: 'other',
            6: 'other'
        }
        series = series.map(mapping)

        if dummy == 'dummy':
            cat_dtype = pd.CategoricalDtype(categories=['none', 'tnb', 'gxy', 'other'])
            series = series.astype(cat_dtype)
            series = pd.get_dummies(series, prefix='j')
            return series.values
        else:
            return np.expand_dims(series.values, axis=1)

    def surgery(df):
        series = np.ones(df['teratme_history'].shape[0])
        series = pd.Series(series)

        for index, row_teratme_history, row_surgery in zip(range(df.shape[0]), df['teratme_history'], df['surgery']):
            if row_teratme_history == 2.0:
                series.loc[index] = row_surgery
            else:
                series.loc[index] = 0.0

        series = series.astype('int')
        mapping = {
            0: 'none',
            1: 'cd',
            2: 'fcd',
            3: 'cd_fcd'
        }
        series = series.map(mapping)

        if dummy == 'dummy':
            cat_dtype = pd.CategoricalDtype(categories=['none', 'cd', 'fcd', 'cd_fcd'])
            series = series.astype(cat_dtype)
            series = pd.get_dummies(series, prefix='surgery')
            return series.values
        else:
            return np.expand_dims(series.values, axis=1)

    def chemotherapy(df):
        series = np.ones(df['chemotherapy'].shape[0])
        series = pd.Series(series)

        for index, row_chemotherapy, row_chemotherapy_regimens in zip(range(df.shape[0]), df['chemotherapy'],
                                                                      df['chemotherapy_regimens']):
            if row_chemotherapy == 2.0:
                series.loc[index] = row_chemotherapy_regimens
            else:
                series.loc[index] = 0.0

        series = series.astype('int')
        mapping = {
            0: 'none',
            1: 'capeox',
            2: 'folfox',
            3: 'other',
            4: 'other'
        }
        series = series.map(mapping)

        if dummy == 'dummy':
            cat_dtype = pd.CategoricalDtype(categories=['none', 'capeox', 'folfox', 'other'])
            series = series.astype(cat_dtype)
            series = pd.get_dummies(series, prefix='chemotherapy')
            return series.values
        else:
            return np.expand_dims(series.values, axis=1)

    def time(series):
        series = series.fillna(0)
        series = series.astype('int')
        return np.expand_dims(series, axis=1)

    def food(series):
        series = series.fillna(6.0)
        series = series.astype('int')
        mapping = {
            1: 'other',
            2: 'other',
            3: 'other',
            4: 'blz',
            5: 'rs',
            6: 'ps'
        }
        series = series.map(mapping)

        if dummy == 'dummy':
            cat_dtype = pd.CategoricalDtype(categories=['blz', 'rs', 'ps', 'other'])
            series = series.astype(cat_dtype)
            series = pd.get_dummies(series, prefix='food')
            return series.values
        else:
            return np.expand_dims(series.values, axis=1)

    def feces(series):
        series.replace('☆', 0, inplace=True)
        series.fillna(0, inplace=True)
        series = series.astype('float')
        return np.expand_dims(series, axis=1)

    def kenel(series):
        series = series.fillna(4.0)
        series = series.astype('int')
        mapping = {
            1: 'other',
            2: 'other',
            3: 'other',
            4: '4',
            5: '5',
            6: '6',
            7: 'other'
        }
        series = series.map(mapping)

        if dummy == 'dummy':
            cat_dtype = pd.CategoricalDtype(categories=['4', '5', '6', 'other'])
            series = series.astype(cat_dtype)
            series = pd.get_dummies(series, prefix='kenel')
            return series.values
        else:
            return np.expand_dims(series.values, axis=1)

    def fuzhang(df):
        series = np.ones(df['fuzhang'].shape[0])
        series = pd.Series(series)

        for index, row_fuzhang, row_chengdu in zip(range(df.shape[0]), df['fuzhang'], df['chengdu']):
            if row_fuzhang == 2.0:
                series.loc[index] = row_chengdu
            else:
                series.loc[index] = 0.0

        series = series.astype('int')
        mapping = {
            0: 'none',
            1: 'qd',
            2: 'zd',
            3: 'zd'
        }
        series = series.map(mapping)

        if dummy == 'dummy':
            cat_dtype = pd.CategoricalDtype(categories=['none', 'qd', 'zd'])
            series = series.astype(cat_dtype)
            series = pd.get_dummies(series, prefix='fuzhang')
            return series.values
        else:
            return np.expand_dims(series.values, axis=1)

    def gzy(df):
        series = np.ones(df['mri'].shape[0])
        series = pd.Series(series)

        for index, row_mri, row_gzy in zip(range(df.shape[0]), df['mri'], df['gzy']):
            if row_mri == 2.0:
                series.loc[index] = row_gzy
            else:
                series.loc[index] = 0.0

        series = series.astype('int')
        mapping = {
            0: 'nk',
            1: 'wzy',
            2: 'zy'
        }
        series = series.map(mapping)

        if dummy == 'dummy':
            cat_dtype = pd.CategoricalDtype(categories=['nk', 'wzy', 'zy'])
            series = series.astype(cat_dtype)
            series = pd.get_dummies(series, prefix='gzy')
            return series.values
        else:
            return np.expand_dims(series.values, axis=1)

    def xr(series):
        series.fillna(2.0, inplace=True)
        series = series.astype('int')
        mapping = {
            1: 1,
            2: 0
        }
        series = series.map(mapping)
        return np.expand_dims(series, axis=1)

    def qcxr(series):
        series.fillna(0, inplace=True)
        series.replace('NK', 1, inplace=True)
        series = series.astype('int')
        return np.expand_dims(series, axis=1)

    def cdzb(df):
        series = np.ones(df['zb'].shape[0])
        series = pd.Series(series)

        for index, row_zb, row_cdzb in zip(range(df.shape[0]), df['zb'], df['cdzb']):
            if row_zb == 2.0:
                series.loc[index] = row_cdzb
            else:
                series.loc[index] = 0.0

        series = series.astype('int')
        mapping = {
            0: 'none',
            1: 'kfffjyec',
            2: 'jggc',
            4: 'other'
        }
        series = series.map(mapping)
        if dummy == 'dummy':
            cat_dtype = pd.CategoricalDtype(categories=['none', 'kfffjyec', 'jggc', 'other'])
            series = series.astype(cat_dtype)
            series = pd.get_dummies(series, prefix='cdzb')
            return series.values
        else:
            return np.expand_dims(series.values, axis=1)

    def db(series):
        series.fillna(7.0, inplace=True)
        series.replace('NK', '7', inplace=True)
        series = series.astype('int')
        mapping = {
            3: 'other',
            4: 'other',
            5: 'other',
            6: 'other',
            7: '7'
        }
        series = series.map(mapping)

        if dummy == 'dummy':
            cat_dtype = pd.CategoricalDtype(categories=['7', 'other'])
            series = series.astype(cat_dtype)
            series = pd.get_dummies(series, prefix='db')
            return series.values
        else:
            return np.expand_dims(series.values, axis=1)

    def bp(series):
        series.fillna(1.0, inplace=True)
        series = series.astype('int')
        mapping = {
            1: 'zc',
            2: 'g',
            3: 'd'
        }
        series = series.map(mapping)

        if dummy == 'dummy':
            cat_dtype = pd.CategoricalDtype(categories=['zc', 'g', 'd'])
            series = series.astype(cat_dtype)
            series = pd.get_dummies(series, prefix='bp')
            return series.values
        else:
            return np.expand_dims(series.values, axis=1)

    def mzsj(df):
        start = df['mzks'].apply(lambda x: datetime.strptime(x, "%Y-%m-%d %H:%M:%S"))
        end = df['mzjs'].apply(lambda x: datetime.strptime(x, "%Y-%m-%d %H:%M:%S"))
        series = (end - start).apply(lambda x: x.seconds / 60)
        return np.expand_dims(series, axis=1)

    def cgsj(df):
        start = df['cgks'].apply(lambda x: datetime.strptime(x, "%Y-%m-%d %H:%M:%S"))
        end = df['cgbg'].apply(lambda x: datetime.strptime(x, "%Y-%m-%d %H:%M:%S"))
        series = (end - start).apply(lambda x: x.seconds / 60)
        return np.expand_dims(series, axis=1)

    def sssj(df):
        start = df['sskssj'].apply(lambda x: datetime.strptime(x, "%Y-%m-%d %H:%M:%S"))
        end = df['ssjssj'].apply(lambda x: datetime.strptime(x, "%Y-%m-%d %H:%M:%S"))
        series = (end - start).apply(lambda x: x.seconds / 60)
        return np.expand_dims(series, axis=1)

    def ssfs(series):
        series.fillna(2.0, inplace=True)
        series = series.astype('int')
        mapping = {
            1: 'kf',
            2: 'fqj',
            3: 'fqjzzkf'
        }
        series = series.map(mapping)

        if dummy == 'dummy':
            cat_dtype = pd.CategoricalDtype(categories=['kf', 'fqj', 'fqjzzkf'])
            series = series.astype(cat_dtype)
            series = pd.get_dummies(series, prefix='ssfs')
            return series.values
        else:
            return np.expand_dims(series.values, axis=1)

    def cdcd(series):
        series.replace('NK', 0, inplace=True)
        series.fillna(0, inplace=True)
        return np.expand_dims(series.astype('float'), axis=1)

    def fhfs(series):
        series.fillna(1.0, inplace=True)
        series = series.astype('int')
        mapping = {
            1: 0,
            2: 1
        }
        series = series.map(mapping)
        return np.expand_dims(series.values, axis=1)

    def sl(series):
        series.fillna(0, inplace=True)
        series = series.astype('int')
        mapping = {
            0: 0,
            1: 0,
            2: 1,
            3: 2,
            4: 3,
            5: 4
        }
        series = series.map(mapping)
        return np.expand_dims(series.values, axis=1)

    def fqzs(series):
        series.fillna(0, inplace=True)
        series = series.astype('int')
        return np.expand_dims(series.values, axis=1)

    def pqzs(series):
        series.fillna(0, inplace=True)
        series = series.astype('int')
        return np.expand_dims(series.values, axis=1)

    def yy(df):
        series = np.ones(df['yy'].shape[0])
        series = pd.Series(series)

        for index, row_yy, row_yymc, row_fnmdjl, row_ltqsjl, row_qtyw in zip(range(df.shape[0]), df['yy'], df['yymc'],
                                                                             df['fnmdjl'], df['ltqsjl'], df['qtyw']):
            if int(row_yy) == 2:
                if row_yymc == 1.0:
                    series.loc[index] = row_fnmdjl
                if row_yymc == 2.0:
                    series.loc[index] = row_ltqsjl
                if row_yymc == 3.0:
                    series.loc[index] = row_qtyw
            else:
                series.loc[index] = 0.0
        series.fillna(0.0, inplace=True)
        series = series.astype('int')
        return np.expand_dims(series, axis=1)

    def event(df):
        series = np.ones(df.shape[0])
        return np.expand_dims(series, axis=1)

    def label(df):
        def process_time(x, label_flag, int_flag, cut='cut'):
            if label_flag == 'day':
                x = x / 1440
                if int_flag == 'i':
                    x = ceil(x)
                else:
                    x = round(x, 1)
                if cut == 'cut':
                    if x > 4:
                        x = 4
                return x

            elif label_flag == 'hour':
                x = x / 60
                if int_flag == 'i':
                    x = ceil(x)
                else:
                    x_float, x_int = modf(x)
                    if 0 < x_float < 0.5:
                        x_float = 0.5
                        x = x_int + x_float
                    if 0.5 < x_float < 1:
                        x_int += 1
                        x = x_int
                if cut == 'cut':
                    if x > 72:
                        x = 72
                return x
            elif label_flag == 'minute':
                return x

        label = df.fillna(INFINITE)
        series = label.apply(lambda x: min(int(x['dycpq']), int(x['pbsk'])), axis=1)

        series = series.apply(process_time, args=(label_flag, int_flag, cut))
        return np.expand_dims(series, axis=1)

    one_hot_zd = zd(df['zd'])
    one_hot_gender = bi_feature(df['gender'].fillna(1.0))
    one_hot_age = continue_feature(df['age'])
    one_hot_height = continue_feature(df['height'])
    one_hot_weight = continue_feature(df['weight'])
    one_hot_index = continue_feature(df['index'])
    one_hot_bmij = bmij(df['bmij'])
    one_hot_nation = nation(df['nation'])
    one_hot_score = continue_feature(df['score'])
    one_hot_j = j(df[['j', 'medical']])
    one_hot_surgery = surgery(df[['teratme_history', 'surgery']])
    one_hot_chemotherapy = chemotherapy(df[['chemotherapy', 'chemotherapy_regimens']])
    one_hot_time = time(df['time'])
    one_hot_target_drugs = bi_feature(df['targeted_drugs'].fillna(1.0))
    one_hot_radiotherapy = bi_feature(df['radiotherapy'].fillna(1.0))
    one_hot_drugs = bi_feature(df['drugs'].fillna(1.0))
    one_hot_food = food(df['food'])
    one_hot_feces = feces(df['feces'])
    one_hot_kenel = kenel(df['kenel'])
    one_hot_nrs_score = continue_feature(df['nrs_score'])
    one_hot_fuzhang = fuzhang(df[['fuzhang', 'chengdu']])
    one_hot_changminyinyoushang = continue_feature(df['changminyin'])
    one_hot_changminyinzuoshang = continue_feature(df['changminyinzuoshang'])
    one_hot_changminyinyouxia = continue_feature(df['changminyinyouxia'])
    one_hot_changminyinzuoxia = continue_feature(df['changminyinzuoxia'])
    one_hot_bsn = bi_feature(df['bsn'].fillna(1.0))
    one_hot_wexner = continue_feature(df['wexner'])
    one_hot_vaizey = continue_feature(df['vaizey'])
    #
    one_hot_hb = continue_feature(df['hb'])
    one_hot_rbc = continue_feature(df['rbc'])
    one_hot_wbc = continue_feature(df['wbc'])
    one_hot_palb = continue_feature(df['palb'])
    one_hot_tp = continue_feature(df['tp'])
    one_hot_alb = continue_feature(df['alb'])
    one_hot_neut = continue_feature(df['neut'])
    #
    one_hot_urea = continue_feature(df['urea'])
    one_hot_crea = continue_feature(df['crea'])
    one_hot_ua = continue_feature(df['ua'])
    one_hot_glu = continue_feature(df['glu'])
    #
    one_hot_k = continue_feature(df['k'])
    one_hot_na = continue_feature(df['na'])
    one_hot_cl = continue_feature(df['cl'])
    one_hot_ca = continue_feature(df['ca'])
    #
    one_hot_gzy = gzy(df[['mri', 'gzy']])
    one_hot_xr = xr(df['xr'])
    one_hot_qcxr = qcxr(df['xrgs'])
    one_hot_cdbb = bi_feature(df['cdbb'].fillna(1.0))
    one_hot_cdzb = cdzb(df[['zb', 'cdzb']])
    one_hot_db = db(df['db'])
    one_hot_hr = continue_feature(df['hr'])
    one_hot_scxyssy = continue_feature(df['scxyssy'])
    one_hot_scxyszy = continue_feature(df['scxyszy'])
    one_hot_bp = bp(df['bp'])
    one_hot_dxl = continue_feature(df['dxl'])
    one_hot_mlpf = continue_feature(df['mlpf'])
    one_hot_xl = continue_feature(df['xl'].fillna(df['xl'].mean()))
    one_hot_xyss = continue_feature(df['xyss'])
    one_hot_xysz = continue_feature(df['xysz'])
    #
    one_hot_mzsj = mzsj(df[['mzks', 'mzjs']])
    one_hot_cgsj = cgsj(df[['cgks', 'cgbg']])
    one_hot_qxsj = continue_feature(df['qxsj'])
    one_hot_sssj = sssj(df[['sskssj', 'ssjssj']])
    one_hot_cxl = continue_feature(df['cxl'])
    one_hot_xbl = continue_feature(df['xbl'])
    one_hot_jtl = continue_feature(df['jtl'])
    one_hot_jtry = continue_feature(df['jtry'])
    one_hot_ssfs = ssfs(df['ssfs'])
    one_hot_ssss = category_feature(
        df[['ssss___1', 'ssss___2', 'ssss___3', 'ssss___4', 'ssss___5', 'ssss___6', 'ssss___7',
            'ssss___8', 'ssss___9', 'ssss__10', 'ssss__11', 'ssss___12', 'ssss___13'
            ]])
    one_hot_cdcd = cdcd(df['cdcd'])
    one_hot_fhfs = fhfs(df['fhfs'])
    one_hot_sl = sl(df['sl'])
    one_hot_cdsl = sl(df['cdsl'])
    one_hot_ylgzl = category_feature(
        df[['ylgzl___1', 'ylgzl___2', 'ylgzl___3', 'ylgzl___4', 'ylgzl___5', 'ylgzl___6', 'ylgzl___7',
            'ylgzl___8', 'ylgzl___9', 'ylgzl___10', 'ylgzl___11'
            ]])
    one_hot_fqzs = fqzs(df['zs'])
    one_hot_pqzs = pqzs(df['pqzs'])
    one_hot_yy = yy(df[['yy', 'yymc', 'fnmdjl', 'ltqsjl', 'qtyw']])
    one_hot_icu = bi_feature(df['icu'].fillna(1.0))
    #
    event = event(df)
    label = label(df[['dycpq', 'pbsk']])

    preoperative_array = [
        one_hot_zd,
        one_hot_gender,
        one_hot_age,
        one_hot_height,
        one_hot_weight,
        one_hot_index,
        one_hot_bmij,
        one_hot_nation,
        one_hot_score,
        one_hot_j,
        one_hot_surgery,
        one_hot_chemotherapy,
        one_hot_time,
        one_hot_target_drugs,
        one_hot_radiotherapy,
        one_hot_drugs,
        one_hot_food,
        one_hot_feces,
        one_hot_kenel,
        one_hot_nrs_score,
        one_hot_fuzhang,
        one_hot_changminyinyoushang,
        one_hot_changminyinzuoshang,
        one_hot_changminyinyouxia,
        one_hot_changminyinzuoxia,
        one_hot_bsn,
        one_hot_wexner,
        one_hot_vaizey,
        one_hot_hb,
        one_hot_rbc,
        one_hot_wbc,
        one_hot_palb,
        one_hot_tp,
        one_hot_alb,
        one_hot_neut,
        one_hot_urea,
        one_hot_crea,
        one_hot_ua,
        one_hot_glu,
        one_hot_k,
        one_hot_na,
        one_hot_cl,
        one_hot_ca,
        one_hot_gzy,
        one_hot_xr,
        one_hot_qcxr,
        one_hot_cdbb,
        one_hot_cdzb,
        one_hot_db,
        one_hot_hr,
        one_hot_scxyssy,
        one_hot_scxyszy,
        one_hot_bp,
        one_hot_dxl,
        one_hot_mlpf,
        one_hot_xl,
        one_hot_xyss,
        one_hot_xysz
    ]

    intraoperative_array = [
        one_hot_mzsj,
        one_hot_cgsj,
        one_hot_qxsj,
        one_hot_sssj,
        one_hot_cxl,
        one_hot_xbl,
        one_hot_jtl,
        one_hot_jtry,
        one_hot_ssfs,
        one_hot_ssss,
        one_hot_cdcd,
        one_hot_fhfs,
        one_hot_sl,
        one_hot_cdsl,
        one_hot_ylgzl,
        one_hot_fqzs,
        one_hot_pqzs,
        one_hot_yy,
        one_hot_icu
    ]

    label_array = [
        event,
        label
    ]

    preoperative_index = [
        'zd_jca', 'zd_zca', 'zd_jca+zca',
        'gender',
        'age',
        'height',
        'weight',
        'index',
        'bmij_zc', 'bmij_xs', 'bmij_cz', 'bmij_fp',
        'nation_hz', 'nation_other',
        'score',
        'j_none', 'j_tnb', 'j_gxy', 'j_other',
        'surgery_none', 'surgery_cd', 'surgery_fcd', 'surgery_cd_fcd',
        'chemotherapy_none', 'chemotherapy_capeox', 'chemotherapy_folfox', 'chemotherapy_other',
        'time',
        'targeted_drugs',
        'radiotherapy',
        'drugs',
        'food_blz', 'food_rs', 'food_ps', 'food_other',
        'feces',
        'kenel_4', 'kenel_5', 'kenel_6', 'kenel_other',
        'nrs_score',
        'fuzhang_none', 'fuzhang_qd', 'fuzhang_zd',
        'changminyinyoushang',
        'changminyinzuoshang',
        'changminyinyouxia',
        'changminyinzuoxia',
        'bsn',
        'wexner',
        'vaizey',
        'hb',
        'rbc',
        'wbc',
        'palb',
        'tp',
        'alb',
        'neut',
        'urea',
        'crea',
        'ua',
        'glu',
        'k',
        'na',
        'cl',
        'ca',
        'gzy_nk', 'gzy_wzy', 'gzy_zy',
        'xr',
        'qcxr',
        'cdbb',
        'cdzb_none', 'cdzb_kfffjyec', 'cdzb_jggc', 'cdzb_other',
        'db_7', 'db_other',
        'hr',
        'scxyssy',
        'scxyszy',
        'bp_zc', 'bp_g', 'bp_d',
        'dxl',
        'mlpf',
        'xl',
        'xyss',
        'xysz'
    ]

    undummy_preoperative_index = [
        'zd',
        'gender',
        'age',
        'height',
        'weight',
        'index',
        'bmij',
        'nation',
        'score',
        'j',
        'surgery',
        'chemotherapy',
        'time',
        'targeted_drugs',
        'radiotherapy',
        'drugs',
        'food',
        'feces',
        'kenel',
        'nrs_score',
        'fuzhang',
        'changminyinyoushang',
        'changminyinzuoshang',
        'changminyinyouxia',
        'changminyinzuoxia',
        'bsn',
        'wexner',
        'vaizey',
        'hb',
        'rbc',
        'wbc',
        'palb',
        'tp',
        'alb',
        'neut',
        'urea',
        'crea',
        'ua',
        'glu',
        'k',
        'na',
        'cl',
        'ca',
        'gzy',
        'xr',
        'qcxr',
        'cdbb',
        'cdzb',
        'db',
        'hr',
        'scxyssy',
        'scxyszy',
        'bp',
        'dxl',
        'mlpf',
        'xl',
        'xyss',
        'xysz'
    ]

    intraoperative_index = [
        'mzsj',
        'cgsj',
        'qxsj',
        'sssj',
        'cxl',
        'xbl',
        'jtl',
        'jtry',
        'ssfs_kf', 'ssfs_fqj', 'ssfs_fqjzzkf',
        'ssss___1', 'ssss___2', 'ssss___3', 'ssss___4', 'ssss___5', 'ssss___6', 'ssss___7', 'ssss___8', 'ssss___9',
        'ssss__10', 'ssss__11', 'ssss___12', 'ssss___13',
        'cdcd',
        'fhfs',
        'sl',
        'cdsl',
        'ylgzl___1', 'ylgzl___2', 'ylgzl___3', 'ylgzl___4', 'ylgzl___5', 'ylgzl___6', 'ylgzl___7', 'ylgzl___8',
        'ylgzl___9', 'ylgzl___10', 'ylgzl___11',
        'fqzs',
        'pqzs',
        'yy',
        'icu'
    ]

    undummy_intraoperative_index = [
        'mzsj',
        'cgsj',
        'qxsj',
        'sssj',
        'cxl',
        'xbl',
        'jtl',
        'jtry',
        'ssfs',
        'ssss___1', 'ssss___2', 'ssss___3', 'ssss___4', 'ssss___5', 'ssss___6', 'ssss___7', 'ssss___8', 'ssss___9',
        'ssss__10', 'ssss__11', 'ssss___12', 'ssss___13',
        'cdcd',
        'fhfs',
        'sl',
        'cdsl',
        'ylgzl___1', 'ylgzl___2', 'ylgzl___3', 'ylgzl___4', 'ylgzl___5', 'ylgzl___6', 'ylgzl___7', 'ylgzl___8',
        'ylgzl___9', 'ylgzl___10', 'ylgzl___11',
        'fqzs',
        'pqzs',
        'yy',
        'icu'
    ]

    label_index = [
        'event',
        'label'
    ]

    if model == 'b':
        arrays = preoperative_array + label_array
        if dummy == 'dummy':
            indexs = preoperative_index + label_index
        else:
            indexs = undummy_preoperative_index + label_index
    elif 'bi' in model:
        arrays = preoperative_array + intraoperative_array + label_array
        if dummy == 'dummy':
            indexs = preoperative_index + intraoperative_index + label_index
        else:
            indexs = undummy_preoperative_index + undummy_intraoperative_index + label_index

    dataset = np.hstack(arrays)
    dataset_df = pd.DataFrame(dataset, columns=indexs)

    if model == 'bia':
        if dummy == 'dummy':
            after = pd.read_csv('data/postoperative_lz_dummy.csv')
        else:
            after = pd.read_csv('data/postoperative_lz_undummy.csv')
        dataset_df = pd.concat([dataset_df, after], axis=1)

    return dataset_df


if __name__ == '__main__':
    # xlsx_to_csv('origin.xlsx', 'origin.csv')
    data = pd.read_csv('origin.csv')
    data = preprocess(data)

    model = 'bia'
    label_flag = 'day'
    int_flag = 'i'
    cut_flag = 'cut'
    dummy = 'dummy'

    data = one_hot(data, model=model, label_flag=label_flag, int_flag=int_flag, cut=cut_flag, dummy=dummy)
    filename = 'data/dataset-{}-{}-{}-{}-{}.csv'.format(model, label_flag, int_flag, cut_flag, dummy)
    data.to_csv(filename, index=None)
